﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using Portal.Core;

namespace Portal.AssetTn.Model
{
    public class AssetTypeCls
    {
        public string AssetTypeId = "";
        public string AssetTypeName = "";
        public string Icon = "";
        public string XmlFieldConfig = "";
        public string XmlConfigSearch = "";
        public string XmlConfigPrint="";
    }

    public class AssetTypeParser
    {
        public static AssetTypeCls CreateInstance()
        {
            //tao 1 class trang....
            AssetTypeCls oAssetType = new AssetTypeCls();
            return oAssetType;
        }

        public static AssetTypeCls ParseFromXml(string XmlData)
        {
            DataSet ds = XmlUtility.GetDataSetFromXml(XmlData);
            AssetTypeCls oAssetType = ParseFromDataRow(ds.Tables[0].Rows[0]);
            return oAssetType;
        }


        public static AssetTypeCls ParseFromDataRow(DataRow dr)
        {
            AssetTypeCls oAssetType = new AssetTypeCls();
            try
            {
                if (dr["AssetTypeId"] != DBNull.Value)
                {
                    oAssetType.AssetTypeId = (string)dr["AssetTypeId"];
                }
            }
            catch { }
            try
            {
                if (dr["AssetTypeName"] != DBNull.Value)
                {
                    oAssetType.AssetTypeName = (string)dr["AssetTypeName"];
                }
            }
            catch { }
            try
            {
                if (dr["Icon"] != DBNull.Value)
                {
                    oAssetType.Icon = (string)dr["Icon"];
                }
            }
            catch { }
            try
            {
                if (dr["XmlFieldConfig"] != DBNull.Value)
                {
                    oAssetType.XmlFieldConfig = (string)dr["XmlFieldConfig"];
                }
            }
            catch { }

            try
            {
                if (dr["XmlConfigSearch"] != DBNull.Value)
                {
                    oAssetType.XmlConfigSearch = (string)dr["XmlConfigSearch"];
                }
            }
            catch { }

            try
            {
                if (dr["XmlConfigPrint"] != DBNull.Value)
                {
                    oAssetType.XmlConfigPrint = (string)dr["XmlConfigPrint"];
                }
            }
            catch { }

            

            return oAssetType;
        }

        public static AssetTypeCls[] ParseFromDataTable(DataTable dtTable)
        {
            AssetTypeCls[] AssetTypes = new AssetTypeCls[dtTable.Rows.Count];
            for (int iIndex = 0; iIndex < dtTable.Rows.Count; iIndex++)
            {
                AssetTypes[iIndex] = ParseFromDataRow(dtTable.Rows[iIndex]);
            }

            return AssetTypes;
        }

        public static DataSet CreateDataset()
        {
            DataSet ds = new DataSet("Asset");
            ds.Tables.Add("Info");
            ds.Tables["Info"].Columns.Add("AssetTypeId");
            ds.Tables["Info"].Columns.Add("AssetTypeName");
            ds.Tables["Info"].Columns.Add("Icon");
            ds.Tables["Info"].Columns.Add("XmlFieldConfig");
            ds.Tables["Info"].Columns.Add("XmlConfigSearch");
            ds.Tables["Info"].Columns.Add("XmlConfigPrint");
            return ds;
        }

        public static string GetXml(AssetTypeCls oAssetType)
        {
            DataSet ds = CreateDataset();
            ds.Tables["Info"].Rows.Add(new object[]{
                        oAssetType.AssetTypeId,
                        oAssetType.AssetTypeName,
                        oAssetType.Icon,
                        oAssetType.XmlFieldConfig,
                        oAssetType.XmlConfigSearch,
                        oAssetType.XmlConfigPrint
                    });
            return ds.GetXml();
        }

        public static string GetXml(DataRow dr)
        {
            AssetTypeCls oAssetType = ParseFromDataRow(dr);
            string Xml = GetXml(oAssetType);
            return Xml;
        }

        public static string GetXml(DataTable dt)
        {
            if (dt == null) return null;
            AssetTypeCls[] AssetTypes = new AssetTypeCls[dt.Rows.Count];
            for (int iIndex = 0; iIndex < dt.Rows.Count; iIndex++)
            {
                AssetTypes[iIndex] = ParseFromDataRow(dt.Rows[iIndex]);
            }
            string Xml = GetXml(AssetTypes);
            return Xml;
        }


        public static string GetXml(AssetTypeCls[] AssetTypes)
        {
            if (AssetTypes == null)
            {
                //neu null thi khoi tao = mang rong de cho do loi...!
                AssetTypes = new AssetTypeCls[0];
            }
            DataSet ds = CreateDataset();
            for (int iIndex = 0; iIndex < AssetTypes.Length; iIndex++)
            {
                AssetTypeCls oAssetType = AssetTypes[iIndex];
                ds.Tables["Info"].Rows.Add(new object[]
                     {
                         oAssetType.AssetTypeId,
                         oAssetType.AssetTypeName,
                         oAssetType.Icon,
                         oAssetType.XmlFieldConfig,
                         oAssetType.XmlConfigSearch,
                         oAssetType.XmlConfigPrint,
                    });
            }
            string XmlData = ds.GetXml();
            return XmlData;
        }

        public static AssetTypeCls[] ParseMultiRecordFromXml(string XmlData)
        {
            DataSet ds = XmlUtility.GetDataSetFromXml(XmlData);
            if (ds.Tables.Count == 0)
            {
                return new AssetTypeCls[0];
            }
            AssetTypeCls[] AssetTypes = new AssetTypeCls[ds.Tables[0].Rows.Count];
            for (int iIndex = 0; iIndex < ds.Tables[0].Rows.Count; iIndex++)
            {
                AssetTypes[iIndex] = ParseFromDataRow(ds.Tables[0].Rows[iIndex]);
            }
            return AssetTypes;
        }
    }
}
